package com.whmnx.main.ui.model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.whmnx.base.bean.LoginBean;
import com.whmnx.base.bean.TreeBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.constants.EventConstants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.view.CommonDialog;
import com.whmnx.main.BR;
import com.whmnx.main.R;
import com.whmnx.main.ui.fragment.CommodityClassifyFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/28 0028 16:58
 * version: 1.0
 * @author Administrator
 */
public class ComClassModel extends ToolbarViewMainModel{
    public CommodityClassifyFragment mFragment;
    private Application application;

    public ComClassModel(@NonNull Application application) {
        super(application);
        this.application = application;
    }


    public void initToolbar(CommodityClassifyFragment commodityClassifyFragment) {
        this.mFragment = commodityClassifyFragment;
        setTitleText("分类管理");
        getCategorytree();
    }

    //给RecyclerView添加ObservableList
    public ObservableList observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<ComcClassItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_com_class);


    //添加商品分类
    public BindingCommand allOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            String s2 = "添加分类";
            new CommonDialog(mFragment.getActivity(), R.style.dialog, s2,1, (dialog, confirm,input,s) -> {
                if (confirm) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("category_Name",input);
                    params.put("userInfo_Id", SPUtils.getInstance().getString(Constants.USERID));
                    HttpClient.post(mFragment.getActivity(), Constants.ADDORUPDATE2, params,new CallBack<LoginBean>() {
                        @Override
                        public void onSuccess(LoginBean result) {
                            getCategorytree();
                        }
                    });
                }
                dialog.dismiss();
            }).show();
        }
    });

    public void getCategorytree() {
        Map<String, Object> params = new HashMap<>();
        params.put("uId", SPUtils.getInstance().getString(Constants.USERID));
        HttpClient.get(mFragment.getActivity(), Constants.GETCATEGORYTREE, params,new CallBack<List<TreeBean>>() {
            @Override
            public void onSuccess(List<TreeBean> treeBeans) {
                observableList.clear();
                for (TreeBean treeBean : treeBeans) {
                    observableList.add(new ComcClassItemViewModel(ComClassModel.this,treeBean));
                }

                RxBus.getDefault().post(EventConstants.ADD2);
            }
        });
    }


    /**
     * 删除条目
     *
     * @param netWorkItemViewModel
     */
    public void deleteItem(ComcClassItemViewModel netWorkItemViewModel) {
        //点击确定，在 observableList 绑定中删除，界面立即刷新
        observableList.remove(netWorkItemViewModel);
        RxBus.getDefault().post(EventConstants.ADD2);
    }
}
